﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using LiveMakkah.Models;
using LiveMakkah.Services;
using ModernHttpClient;
using Newtonsoft.Json;
using Xamarin.Forms;

[assembly: Dependency(typeof (ChannelListService))]
namespace LiveMakkah.Services
{
    public class ChannelListService : IChannelListService
    {
        private System.Net.Http.HttpClient _baseClient;

        private System.Net.Http.HttpClient BaseClient
        {
            get
            {
                _baseClient = new System.Net.Http.HttpClient(new NativeMessageHandler());
                _baseClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                return _baseClient;
            }
        }

        public async Task<List<FarahnakModel>> GetChannels() 
        {
                HttpResponseMessage res =
                    await BaseClient.GetAsync(ServiceConstants.ApiChannels);
                res.EnsureSuccessStatusCode();
                string json = await res.Content.ReadAsStringAsync();
                if (string.IsNullOrEmpty(json)) throw new Exception("Return content was empty :(");
                return JsonConvert.DeserializeObject<List<FarahnakModel>>(json);
        }
    }
}